using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text.RegularExpressions;
namespace DAL
{
    public class YCodeTransforms : DAL.DataManager<YCodeTransform>
    {
        private static YCodeTransforms Instance = new YCodeTransforms();
        static YCodeTransforms() { }

        public override string TableName
        {
            get { return @"fstdt_ycode_transforms"; }
        }

        protected override void CopyToItem(DataRow Row, YCodeTransform Item)
        {
            Item.YCode = YCodes.Item(Convert.ToInt64(Row["YCodeID"]));


            Item.Position = Convert.ToInt64(Row["Position"]);


            Item.Pattern = Convert.ToString(Row["Pattern"]);


            Item.Replacement = Convert.ToString(Row["Replacement"]);
        }

        protected override void CopyFromItem(DataManagerQuery query, YCodeTransform Item)
        {
            query.AddParam("YCodeID", Item.YCode.ID);


            query.AddParam("Position", Item.Position);


            query.AddParam("Pattern", Item.Pattern);


            query.AddParam("Replacement", Item.Replacement);
        }

        public static YCodeTransform Item(Int64 id)
        {
            return Instance.GetItem(id);
        }

        public static List<YCodeTransform> All()
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddSort("Position", Sort.Asc);
            return Instance.GetItems(query);
        }

        public static void Add(YCodeTransform Item)
        {
            Instance.Insert(Item);
            Item.Position = Item.ID;
            Save(Item);
        }

        public static void Save(YCodeTransform Item)
        {
            Instance.Update(Item);
        }

        public static void Remove(YCodeTransform Item)
        {
            Instance.Delete(Item);
        }

        public static void Move(YCodeTransform Item, Direction dir)
        {
            Helpers.PositionHelper<YCodeTransform> position = new Helpers.PositionHelper<YCodeTransform>("YCodeID", Item.YCode);
            position.MoveItem(Instance, Item, dir);
        }

        public static List<YCodeTransform> GetByYCode(YCode parent)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("YCodeID", parent.ID);
            query.AddSort("Position", Sort.Asc);
            return Instance.GetItems(query);
        }
    }

    public class YCodeTransform : DAL.DataManagerRow, IRowPosition
    {
        private YCode _YCode;
        public YCode YCode
        {
            get
            {
                return _YCode;
            }
            set
            {
                _YCode = value;
            }
        }


        private Int64 _Position;
        public Int64 Position
        {
            get
            {
                return _Position;
            }
            set
            {
                _Position = value;
            }
        }


        private string _Pattern;
        public string Pattern
        {
            get
            {
                return _Pattern;
            }
            set
            {
                _Pattern = value;
            }
        }


        private string _Replacement;
        public string Replacement
        {
            get
            {
                return _Replacement;
            }
            set
            {
                _Replacement = value;
            }
        }

        private Regex _formatter;
        public Regex Formatter
        {
            get
            {
                if (_formatter == null)
                {
                    _formatter = new Regex(Pattern,
                        RegexOptions.Compiled |
                        RegexOptions.IgnoreCase |
                        RegexOptions.IgnorePatternWhitespace |
                        RegexOptions.Singleline);
                }
                return _formatter;
            }
        }
    }
}